package com.yangcangclub.commons.utils;

import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;

import java.util.regex.Pattern;

/**
 * 参数校验工具类
 */
public  final class ParamValidateUtil {

    private ParamValidateUtil(){}
    private static final ParamValidateUtil validator = new ParamValidateUtil();


    /**
     * "^[A-Za-z0-9_]{4,16}$"
     */
    private static final String PASSWORK_REGEX ="^[A-Za-z0-9_]{4,16}$";// 密码









    public static  boolean validate(String value,String regEx){
        if(StringUtil.isFullEmpty(value)) throw new ApolloException(MessageEnum.PARMA_IS_EMPTY);
        return Pattern.compile(regEx).matcher(value).find();
    }

    /**
     *无返值校验
     */
    public static ParamValidateUtil  validateNoResp(String value,String regEx){
        boolean b=validate(value,regEx);
        if(!b) throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);
        return validator;
    }



    public static void main(String[] args) {
        try{
            ParamValidateUtil.validateNoResp("aa", "^[a-z]+$")
                    .validateNoResp("d1d", "^[a-z]+$");

        }catch(Exception e){
            ApolloException es=(ApolloException) e;
            e.printStackTrace();
            System.out.println(es.getMessageEnum().getDesc());
        }
    }



}
